<?php
/**
 * User: Hufeng
 * Date: 2017/08/29 19:48
 * Desc: 交易所Model
 */
namespace App\Model;
use SasPHP\SqlModel;

class TradeKlineModel extends SqlModel{
    protected $dbName = 'trade_kline';

    public function __construct(){
        parent::__construct('DB_SERVER');
    }

    function insertData($data){
        $this->dbConn->insert($this->dbName,$data);
        return $this->dbConn->id();
    }

    function editData($where,$data){
        $this->dbConn->update($this->dbName,$data,$where);
    }

    function delData($data){
        $this->dbConn->delete($this->dbName, $data);
    }

    function findData($where,$fields='*'){
        $res = $this->dbConn->get($this->dbName,$fields,$where);
        return $res;
    }

    function queryData($where,$fields='*'){
        $res = $this->dbConn->select($this->dbName,$fields,$where);
        return $res;
    }
    //获取统计数据
    public function getTotalNum($where){
        $res = $this->dbConn->count($this->dbName,$where);
        return $res;
    }
    //sum数据
    public function sumData($where,$param){
        $res = $this->dbConn->sum($this->dbName,$param,$where);
        return $res;
    }

    //处理K线图
    public function dealKline($stime,$etime,$ktype,$type,$timeNow,$param){
        $where = ['sid'=>$param['sid'],'addtime[>=]'=>$stime,'addtime[<=]'=>$etime,'kline_type'=>$ktype,'type'=>$type];
        $res = $this->findData($where);
        if($res){
            $editData = [
                'close'=>$param['dealPrice'],
                'high'=>$param['dealPrice']>$res['high'] ? $param['dealPrice'] : $res['high'],
                'lower'=>$param['dealPrice']<$res['lower'] ? $param['dealPrice'] : $res['lower'],
                'deal_num'=>$param['dealNum']+$res['deal_num'],
                'deal_count'=>$res['deal_count']+1,
                'deal_account'=>round($res['deal_account']+($param['dealAvgPrice']*$param['dealNum']),2),
                'edittime'=>$timeNow,
            ];
            $this->editData(['id'=>$res['id']],$editData);
        }else{
            $inData = [
                'sid'=>$param['sid'],
                'open'=>$param['dealPrice'],
                'close'=>$param['dealPrice'],
                'high'=>$param['dealPrice'],
                'lower'=>$param['dealPrice'],
                'deal_num'=>$param['dealNum'],
                'deal_count'=>1,
                'deal_account'=>round($param['dealAvgPrice']*$param['dealNum'],2),
                'addtime'=>$timeNow,
                'edittime'=>$timeNow,
                'kline_type'=>$ktype,
                'type'=>$type
            ];
            $this->insertData($inData);
        }
    }

    //昨日综合指数
    public function compytQuota($tpId){
        $today = date('Y-m-d');
        $queryData = $this->findData(['sid'=>$tpId,'kline_type'=>1,'type'=>2,'edittime[<]'=>$today,'ORDER'=>['id'=>'DESC']]);
        if(!$queryData){
            $queryData = $this->findData(['sid'=>$tpId,'kline_type'=>1,'type'=>2,'edittime[>=]'=>$today,'ORDER'=>['id'=>'ASC']]);
        }
        if(!$queryData){
            return 0;
        }else{
            return $queryData['close'];
        }
    }
}
